<?php

use Think\Model\RelationModel;
/**
 * 支付 逻辑定义
 * Class unionpayPayment
 * @package Home\Payment
 */

class yuntongpay extends RelationModel
{    
    public $tableName = 'plugin'; // 插件表        
    public $shan_config = array();// 银联支付配置参数
    
    /**
     * 析构流函数
     */
    public function  __construct() {   
        parent::__construct();
        
        $paymentPlugin = M('Plugin')->where("code='yuntongpay' and  type = 'payment' ")->find(); // 找到支付插件的配置
        $config_value = unserialize($paymentPlugin['config_value']); // 配置反序列化 
		  
        $this->shan_config['user_seller']= $config_value['user_seller']; // 商户号
        $this->shan_config['partner']= $config_value['partner'];// 
        $this->shan_config['key']  = $config_value['ukey'];//
        $this->shan_config['notify_url'] = SITE_URL.U('Payment/notifyUrl',array('pay_code'=>'yuntongpay'));
        $this->shan_config['return_url'] = SITE_URL.U('Payment/returnUrl',array('pay_code'=>'yuntongpay'));	//企业网银密码
       
    }    
    /**
     * 生成支付代码
     * @param   array   $order      订单信息
     * @param   array   $config_value    支付方式信息
     */
    function get_code($order, $config_value)
    {  


		include_once $_SERVER ['DOCUMENT_ROOT'] . '/plugins/payment/yuntongpay/lib/shanpayfunction.php';
		$ytp= new Yuntong();
		$parameter = array(
		"partner" => $this->shan_config['partner'],
        "user_seller"  => $this->shan_config['user_seller'],
		"out_order_no"	=> $order['order_sn'],
		"subject"	=> $order['nickname'].$order['pay_name'],
		"total_fee"	=> $order['account'],
		"body"	=> $order['nickname'].$order['pay_name'],
		"notify_url"	=> $this->shan_config['notify_url'],
		"return_url"	=> $this->shan_config['return_url']
);

//建立请求
$html_text = $ytp->buildRequestFormShan($parameter, $this->shan_config['key']);
return $html_text;
    }
    
    /**
     * 服务器点对点响应操作给支付接口方调用
     * 
     */
    function response()
    {               
        include_once $_SERVER ['DOCUMENT_ROOT'] . '/plugins/payment/yuntongpay/lib/shanpayfunction.php';  // 请求返回 
	$ytp= new Yuntong();
		
    $shanNotify = $ytp->md5VerifyShan($_REQUEST['out_order_no'],$_REQUEST['total_fee'],$_REQUEST['trade_status'],$_REQUEST['sign'],$this->shan_config['key'],$this->shan_config['partner']);
  
	if($shanNotify) {//验证成功
	if($_REQUEST['trade_status']=='TRADE_SUCCESS'){
		 $order_sn = $out_trade_no = $_REQUEST['out_order_no']; //商户订单号
		  update_pay_status($order_sn); // 修改订单支付状态
		   
			
			//云通付交易号
			$trade_no = $_REQUEST['trade_no'];
			//价格
			$price=$_REQUEST['total_fee'];
			// var_dump($_REQUEST);
		}
		echo 'success';

}else {
   //验证失败
    echo "fail";//请不要修改或删除
}  
	  
	
    }
    
    /**
     * 页面跳转响应操作给支付接口方调用
     */
    function respond2()
    {
		include_once $_SERVER ['DOCUMENT_ROOT'] . '/plugins/payment/yuntongpay/lib/shanpayfunction.php';  // 请求返回 
	$ytp= new Yuntong();
		
    $shanNotify = $ytp->md5VerifyShan($_REQUEST['out_order_no'],$_REQUEST['total_fee'],$_REQUEST['trade_status'],$_REQUEST['sign'],$this->shan_config['key'],$this->shan_config['partner']);
	  if($shanNotify) {//验证成功
	if($_REQUEST['trade_status']=='TRADE_SUCCESS'){
		  $order_sn = $out_trade_no = $_REQUEST['out_order_no']; //商户订单号
		  update_pay_status($order_sn); // 修改订单支付状态
		 
			//商户订单号
			$out_trade_no = $_REQUEST['out_order_no'];
			//云通付交易号
			$trade_no = $_REQUEST['trade_no'];
			//价格
			$price=$_REQUEST['total_fee'];
			
			return array('status'=>1,'order_sn'=>$order_sn);//跳转至成功页面
		}else{
			return array('status'=>0,'order_sn'=>$order_sn); //跳转至失败页面
		}

}
else {
    //验证失败
   return array('status'=>0,'order_sn'=>$_POST['orderId']);//跳转至失败页面
}

    }
    
}